#include "solar/trunk/NTitleBar.h"
//**Invalid because this function was set before the bitmap, when the dictionary key value is empty, resulting in an empty loop

NTitleBar::NTitleBar(QWidget *parent) : QMainWindow(parent,
                     QWidget().windowFlags() |
                     Qt::Window |
                     Qt::WindowMinimizeButtonHint |
                     Qt::FramelessWindowHint)

    ,win_radius(4)
    ,ico_margin_(0)
    ,backups_mask(QColor(94,75,160))
    {
        d_filler_enable(FillerType::BACKCOLOR, true);
        linear_orien = NT::ORIEN_VERTICAL;
        qRegisterMetaType<QPair<QMap<QString, QPoint>, int>>("QPair<QMap<QString, QPoint>, int>");
        TT_cloakLHbox->addWidget(TT_sysIcoLabel, 0, Qt::AlignVCenter);
        TT_cloakLHbox->addWidget(TT_captionLabel, 1, Qt::AlignVCenter);
        TT_cloakLHbox->addWidget(TT_piazeeWin, 0, Qt::AlignVCenter);

        TT_cloakRHbox->addWidget(TT_normalLabel, 0, Qt::AlignVCenter);
        TT_cloakRHbox->addWidget(TT_minwinLabel, 0, Qt::AlignVCenter);
        TT_cloakRHbox->addWidget(TT_maxwinLabel, 0, Qt::AlignVCenter);
        TT_cloakRHbox->addWidget(TT_closeLabel, 0, Qt::AlignVCenter);
        TT_cloakHbox->addLayout(TT_cloakLHbox, Qt::AlignVCenter);
        TT_cloakHbox->addLayout(TT_cloakRHbox, Qt::AlignTop);
        TT_cloakHbox->setSpacing(20);
        TT_cloakHbox->setContentsMargins(0,0,0,0);

        setCentralWidget(TT_cloakWidget);
        setAttribute(Qt::WA_TranslucentBackground);
        setAttribute(Qt::WA_TransparentForMouseEvents);
        setAttribute(Qt::WA_DeleteOnClose);
        _switch.insert("stretch-move-enable", true);

        TT_normalColorGroup = {{MSEVKEY.WIN_NORMAL, QColor(198,236,246)},
                               {MSEVKEY.WIN_ENTER, QColor(222,225,232)}};

        TT_maxwinColorGroup = {{MSEVKEY.WIN_NORMAL, QColor(159,159,255)},
                               {MSEVKEY.WIN_ENTER, QColor(222,225,232)}};

        TT_minwinColorGroup ={{MSEVKEY.WIN_NORMAL, QColor(255,197,90)},
                              {MSEVKEY.WIN_ENTER, QColor(222,225,232)}};

        TT_closeColorGroup = {{MSEVKEY.WIN_NORMAL, QColor(255,159,159)},
                              {MSEVKEY.WIN_ENTER, QColor(255,92,92)}};

        installEventFilter(this);
        d_layout_specing();
        d_basicbuttom_configur(NT::PIXLOAD_IGNORERATIO_LOWER, NT::DRAWTYPE_ROUND, 3, 1.4);
        d_basicbuttom_color(BasicButtom::Rroof_Normal, TT_normalColorGroup);
        d_basicbuttom_color(BasicButtom::Rroof_Min, TT_minwinColorGroup);
        d_basicbuttom_color(BasicButtom::Rroof_Max, TT_maxwinColorGroup);
        d_basicbuttom_color(BasicButtom::Rroof_Exit, TT_closeColorGroup);
        TT_cloakLHbox->setContentsMargins(5,0,20,0);
        TT_piazeeWin->setContentsMargins(20,0,0,0);
        TT_cloakRHbox->setContentsMargins(0,0,6,0);
        TT_cloakHbox->setContentsMargins(0,0,0,0);
        TT_sysIcoLabel->doconfigur_adjustedwidth(true);
        TT_cloakLHbox->setSpacing(5);

        connect(TT_normalLabel, &NinsipidView::msev_rrls, this, &NTitleBar::tt_msev_nrrls);
        connect(TT_normalLabel, &NinsipidView::msev_rls, this, [&](){emit tt_cdmsev_rls(BasicButtom::Rroof_Normal);});
        connect(TT_maxwinLabel, &NinsipidView::msev_rls, this, [&](){emit tt_cdmsev_rls(BasicButtom::Rroof_Max);});
        connect(TT_minwinLabel, &NinsipidView::msev_rls, this, [&](){emit tt_cdmsev_rls(BasicButtom::Rroof_Min);});
        connect(TT_closeLabel, &NinsipidView::msev_rls, this,  [&](){emit tt_cdmsev_rls(BasicButtom::Rroof_Exit);});
        connect(TT_captionLabel, &NinsipidView::msev_pre, this,[&](){notify(ButtomType::Mouse_Press); emit tt_ttxt_pre();});
        connect(TT_captionLabel, &NinsipidView::msev_rls, this,[&](){notify(ButtomType::Mouse_Release);});
        connect(TT_sysIcoLabel, &NinsipidView::msev_pre, this, [&](QMouseEvent *){emit tt_ico_pre();});
    }

NTitleBar::~NTitleBar(){
    delete TT_piazeeWin;
}

void NTitleBar::d_painter_effectrange(QRect _range, QList<QColor> _flrcolor){
    effect_range = _range;
    back_color_map["filler_color"] = _flrcolor;
}

void NTitleBar::d_margin_radius(QMargins margin, int radius){
    win_radius = radius;
    setContentsMargins(margin);
    update();
}

void NTitleBar::d_caption(QString text, QFont font, QColor color){
    TT_captionLabel->doconfigur_text(text);
    TT_captionLabel->setFixedHeight(height());
    TT_captionLabel->doconfigur_fontgroup({{MSEVKEY.WIN_NORMAL, font}});
    d_caption_text_color({{MSEVKEY.WIN_NORMAL, color}});
    setProperty("caption-font", font);
    setProperty("caption-color", color);
}

void NTitleBar::d_caption(QString text, QFont font){
    QColor color = property("caption-color").value<QColor>();
    d_caption(text, font, color);
}

void NTitleBar::d_caption(QString text){
    QFont font = property("caption-font").value<QFont>();
    QColor color = property("caption-color").value<QColor>();
    d_caption(text, font, color);
}

void NTitleBar::d_captionico(QString curtain, NT::CST_BRIGHT_SPOT drawType, double _ico_ratio, int margins){
    int szH = ceil((double)height() / _ico_ratio);
    TT_sysIcoLabel->setFixedHeight(szH);
    TT_sysIcoLabel->doconfigur_pixmap(curtain);
    TT_sysIcoLabel->doconfigur_imgsetting(drawType, NT::PIXLOAD_KEEPRATIO, margins);
    TT_sysIcoLabel->doconfigur_adjustedwidth(true);
}

void NTitleBar::d_captionico(QString curtain, double _ico_ratio, int margins){
    int szH = ceil((double)height() / _ico_ratio);
    TT_sysIcoLabel->setFixedHeight(szH);
    TT_sysIcoLabel->doconfigur_pixmap(curtain);
    TT_sysIcoLabel->doconfigur_imgsetting(NT::DRAWTYPE_RECT, NT::PIXLOAD_KEEPRATIO, margins);
    TT_sysIcoLabel->doconfigur_adjustedwidth(true);
}

void NTitleBar::d_captionico(QString curtain, int margins){
    int szH = ceil((double)height() / 1);
    TT_sysIcoLabel->setFixedHeight(szH);
    TT_sysIcoLabel->doconfigur_pixmap(curtain);
    TT_sysIcoLabel->doconfigur_imgsetting(NT::DRAWTYPE_RECT, NT::PIXLOAD_KEEPRATIO, margins);
    TT_sysIcoLabel->doconfigur_adjustedwidth(true);
}

void NTitleBar::d_caption_text_color(QMap<QString, QColor> colorOption){
    TT_captionLabel->doconfigur_colorgroup(colorOption);
}

void NTitleBar::d_basicbuttom_color(BasicButtom buttom, QString _key, QColor upcolor, bool overturn){
    switch (buttom)
    {
        case Rroof_Normal:
            if (overturn) TT_normalColorGroup.clear();
            TT_normalColorGroup[_key] = upcolor;
            TT_normalLabel->doconfigur_backcolorgroup(TT_normalColorGroup);
            break;
        case Rroof_Min:
            if (overturn) TT_minwinColorGroup.clear();
            TT_minwinColorGroup[_key] = upcolor;
            TT_minwinLabel->doconfigur_backcolorgroup(TT_minwinColorGroup);
            break;
        case Rroof_Max:
            if (overturn) TT_maxwinColorGroup.clear();
            TT_maxwinColorGroup[_key] = upcolor;
            TT_maxwinLabel->doconfigur_backcolorgroup(TT_maxwinColorGroup);
            break;
        case Rroof_Exit:
            if (overturn) TT_closeColorGroup.clear();
            TT_closeColorGroup[_key] = upcolor;
            TT_closeLabel->doconfigur_backcolorgroup(TT_closeColorGroup);
            break;
    }
}

void NTitleBar::d_basicbuttom_color(BasicButtom buttom, QMap<QString, QColor> colorOption){
    switch (buttom)
    {
        case Rroof_Normal:
            TT_normalLabel->doconfigur_backcolorgroup(colorOption);
            break;
        case Rroof_Min:
            TT_minwinLabel->doconfigur_backcolorgroup(colorOption);
            break;
        case Rroof_Max:
            TT_maxwinLabel->doconfigur_backcolorgroup(colorOption);
            break;
        case Rroof_Exit:
            TT_closeLabel->doconfigur_backcolorgroup(colorOption);
            break;
    }
}

void NTitleBar::d_basicbuttom_pixmap(BasicButtom buttom, QString curtain){
    switch (buttom)
    {
        case Rroof_Normal:
            TT_normalLabel->doconfigur_pixmap(curtain);
            break;
        case Rroof_Min:
            TT_minwinLabel->doconfigur_pixmap(curtain);
            break;
        case Rroof_Max:
            TT_maxwinLabel->doconfigur_pixmap(curtain);
            break;
        case Rroof_Exit:
            TT_closeLabel->doconfigur_pixmap(curtain);
            break;
    }
}

void NTitleBar::d_basicbuttom_pixmap(BasicButtom buttom, QPixmap pixmap){
    switch (buttom)
    {
        case Rroof_Normal:
            TT_normalLabel->doconfigur_pixmap(pixmap);
            break;
        case Rroof_Min:
            TT_minwinLabel->doconfigur_pixmap(pixmap);
            break;
        case Rroof_Max:
            TT_maxwinLabel->doconfigur_pixmap(pixmap);
            break;
        case Rroof_Exit:
            TT_closeLabel->doconfigur_pixmap(pixmap);
            break;
    }
}

void NTitleBar::d_basicbuttom_freepixmap(GraphType Type, int graphthickness, QColor graphcolor){
    QPixmap norpix;
    QPixmap minpix;
    QPixmap maxpix;
    QPixmap exipix;
    norpix = freepixmap(QSize(height(), height()), graphthickness, graphcolor, NORWIN);
    minpix = freepixmap(QSize(height(), height()), graphthickness, graphcolor, MINWIN);
    maxpix = freepixmap(QSize(height(), height()), graphthickness, graphcolor, MAXWIN);
    exipix = freepixmap(QSize(height(), height()), graphthickness, graphcolor, QUITE);
    switch (Type)
    {
        case NORWIN:
            TT_normalLabel->doconfigur_pixmap(norpix);
            break;
        case MINWIN:
            TT_minwinLabel->doconfigur_pixmap(minpix);
            break;
        case MAXWIN:
            TT_maxwinLabel->doconfigur_pixmap(maxpix);
            break;
        case QUITE:
            TT_closeLabel->doconfigur_pixmap(exipix);
            break;
    }
}

void NTitleBar::d_basicbuttom_configur(NT::WIN_ATTRIBUTE ratioType, NT::CST_BRIGHT_SPOT SyncdrawType, int _rect_margins, double _but_ratio){
    if (ratioType == NT::PIXLOAD_IGNORERATIO_LOWER)
    {
        int szQ = (double)height() / _but_ratio;
        TT_normalLabel->setFixedSize(szQ, szQ);
        TT_maxwinLabel->setFixedSize(szQ, szQ);
        TT_minwinLabel->setFixedSize(szQ, szQ);
        TT_closeLabel->setFixedSize(szQ, szQ);
    }
    else if (ratioType == NT::PIXLOAD_KEEPRATIO)
    {
        TT_normalLabel->doconfigur_adjustedwidth(true);
        TT_maxwinLabel->doconfigur_adjustedwidth(true);
        TT_minwinLabel->doconfigur_adjustedwidth(true);
        TT_closeLabel->doconfigur_adjustedwidth(true);
    }
    TT_normalLabel->doconfigur_brighttype(SyncdrawType);
    TT_maxwinLabel->doconfigur_brighttype(SyncdrawType);
    TT_minwinLabel->doconfigur_brighttype(SyncdrawType);
    TT_closeLabel->doconfigur_brighttype(SyncdrawType);
    TT_normalLabel->doconfigur_imgsetting(NT::DRAWTYPE_RECT, NT::PIXLOAD_IGNORERATIO_LOWER, _rect_margins);
    TT_maxwinLabel->doconfigur_imgsetting(NT::DRAWTYPE_RECT, NT::PIXLOAD_IGNORERATIO_LOWER, _rect_margins);
    TT_minwinLabel->doconfigur_imgsetting(NT::DRAWTYPE_RECT, NT::PIXLOAD_IGNORERATIO_LOWER, _rect_margins);
    TT_closeLabel->doconfigur_imgsetting(NT::DRAWTYPE_RECT, NT::PIXLOAD_IGNORERATIO_LOWER, _rect_margins);
}

void NTitleBar::d_basicbuttom_where(BasicButtom buttom, bool enable){
    switch (buttom)
    {
        case Rroof_Normal:
            TT_normalLabel->setVisible(enable);
            break;
        case Rroof_Min:
            TT_minwinLabel->setVisible(enable);
            break;
        case Rroof_Max:
            TT_maxwinLabel->setVisible(enable);
            break;
        case Rroof_Exit:
            TT_closeLabel->setVisible(enable);
            break;
    }
}

void NTitleBar::d_basicbuttom_where(BasicButtom buttom, QStringList keys,  bool enable){
    QMap<QString, QColor> tempropery;
    switch (buttom)
    {
        case Rroof_Normal:
            tempropery.swap(TT_normalLabel->color(NinsipidView::BACK));
            if (enable)
            {
                foreach(QString key, keys) tempropery.insert(key, TT_normalColorGroup[key]);
            }
            else
            {
                foreach(QString key, keys) tempropery.remove(key);
            }
            d_basicbuttom_color(BasicButtom::Rroof_Normal, tempropery);
            break;
        case Rroof_Min:
            tempropery.swap(TT_minwinLabel->color(NinsipidView::BACK));
            if (enable)
            {
                foreach(QString key, keys) tempropery.insert(key, TT_minwinColorGroup[key]);
            }
            else
            {
                foreach(QString key, keys) tempropery.remove(key);
            }
            d_basicbuttom_color(BasicButtom::Rroof_Min, tempropery);
            break;
        case Rroof_Max:
            tempropery.swap(TT_maxwinLabel->color(NinsipidView::BACK));
            if (enable)
            {
                foreach(QString key, keys) tempropery.insert(key, TT_maxwinColorGroup[key]);
            }
            else
            {
                foreach(QString key, keys) tempropery.remove(key);
            }
            d_basicbuttom_color(BasicButtom::Rroof_Max, tempropery);
            break;
        case Rroof_Exit:
            tempropery.swap(TT_closeLabel->color(NinsipidView::BACK));
            if (enable)
            {
                foreach(QString key, keys) tempropery.insert(key, TT_closeColorGroup[key]);
            }
            else
            {
                foreach(QString key, keys) tempropery.remove(key);
            }
            d_basicbuttom_color(BasicButtom::Rroof_Exit, tempropery);
            break;
    }
}

void NTitleBar::d_basicbuttom_where(QList<BasicButtom> buttoms, QStringList keys,  bool enable){
    foreach(BasicButtom buttom, buttoms) d_basicbuttom_where(buttom, keys, enable);
}

void NTitleBar::d_basicbuttom_where(QList<BasicButtom> buttom, bool enable){
    foreach(BasicButtom _buttom, buttom)
    {
        d_basicbuttom_where(_buttom, enable);
    }
}

void NTitleBar::d_systemico_configur(NT::WIN_ATTRIBUTE ratioType, double _but_ratio){
    if (ratioType == NT::PIXLOAD_IGNORERATIO_LOWER)
    {
        int szQ = (double)height() / _but_ratio;
        TT_sysIcoLabel->setFixedSize(szQ, szQ);
    }
    else if (ratioType == NT::PIXLOAD_KEEPRATIO)
    {
         TT_sysIcoLabel->doconfigur_adjustedwidth(true);
    }
}

void NTitleBar::d_layout_specing(uint _left_, uint _judge_, uint _right_){
    TT_cloakHbox->setSpacing(_judge_);
    TT_cloakLHbox->setSpacing(_left_);
    TT_cloakRHbox->setSpacing(_right_);
}

void NTitleBar::paintEvent(QPaintEvent *Event){
    QPainter captainPainter(this);
    captainPainter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
    QPainterPath path;
    path.addRoundedRect(rect().adjusted(0, 0, 0, win_radius), win_radius, win_radius);
    captainPainter.setClipPath(path);
    QList<QColor> standar_colors = back_color_map["standar_color"];
    QList<QColor> filler_colors = back_color_map["filler_color"];
    QLinearGradient lineGrandient;
    int rang_w = effect_range.width();
    int rang_h = effect_range.height();
    QRect offectrect;
    QRect realrect = captainPainter.viewport();
    if (!filler_colors.isEmpty())
    {
        painterLinear(captainPainter, realrect, linear_orien, filler_colors);
    }
    if (RdoMap_FillerType[FillerType::BACKCOLOR])
    {
        int apply_w = rang_w;
        int apply_y = rang_h;
        if (!rang_w) apply_w = realrect.width();
        if (!rang_h) apply_y = realrect.height();
        offectrect = QRect(effect_range.x(), effect_range.y(), apply_w, apply_y);
    }
    else
    {
        offectrect = captainPainter.viewport();
    }

    captainPainter.setPen(QPen(Qt::transparent));
    if (RdoMap_FillerType[FillerType::BACKCOLOR] && !standar_colors.isEmpty())
    {
        painterLinear(captainPainter, offectrect, linear_orien, standar_colors);

    }
    else if (RdoMap_FillerType[FillerType::BACKPIXMAP] && !back_pixmap.isNull())
    {
        captainPainter.drawPixmap(offectrect, back_pixmap);
    }
    else
    {
        captainPainter.fillRect(offectrect, backups_mask);
    }
    QWidget::paintEvent(Event);

}

void NTitleBar::painterLinear(QPainter &painter, QRect offectrect, NT::CST_BRIGHT_SPOT orien, QList<QColor> _colors){
    QLinearGradient lineGrandient;
    double DetailAddtion = 0;
    double RelativeStep = 1.0 / (double)(_colors.size() - 1);
    if (orien == NT::ORIEN_HORIZONTAL)
    {
        lineGrandient = QLinearGradient(0, 0, offectrect.width(), 0);
    }
    else
    {
        lineGrandient = QLinearGradient(0, 0, 0, offectrect.height());
    }
    foreach(QColor color, _colors)
    {
        lineGrandient.setColorAt(DetailAddtion, color);
        lineGrandient.setSpread(QGradient::ReflectSpread);
        DetailAddtion += RelativeStep;
    }
    painter.fillRect(offectrect, lineGrandient);
}

bool NTitleBar::eventFilter(QObject *watched, QEvent *Event){
    QList<QEvent::Type> acceptTypes({QEvent::HoverMove,
                                     QEvent::MouseButtonDblClick,
                                     QEvent::MouseButtonPress,
                                     QEvent::MouseButtonRelease,
                                     QEvent::Move});
    if (acceptTypes.contains(Event->type()))
    {
        QMouseEvent *mouseEv = static_cast<QMouseEvent*>(Event);
        if (Event->type() == QEvent::HoverMove)
        {
            QPoint _pos = mouseEv->pos();

            notify(ButtomType::Mouse_Move, _pos);
        }
        else if (Event->type() == QEvent::MouseButtonDblClick)
        {
            notify(ButtomType::Mouse_Double);
        }
        else
        {
            if (mouseEv->button() == Qt::LeftButton &&
                Event->type() == QEvent::MouseButtonPress &&
                mouseEv->pos().x() < width() - TT_cloakRHbox->sizeHint().width()) //修复异常渐变显示和异常坐标的问题***************鼠标从外部窗口移动至标题栏，会意外触发点击事件
            {
                notify(ButtomType::Mouse_Press);
            }
            if (mouseEv->button() == Qt::LeftButton &&
                Event->type() == QEvent::MouseButtonRelease)
                {
                    notify(ButtomType::Mouse_Release);
                }
        }
    }
    else if (Event->type() == QEvent::Show)
    {
        if (!back_pixmap.isNull())
        {
            back_pixmap = back_pixmap.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        }
    }
    else if (Event->type() == QEvent::WindowActivate)
    {
        if (TT_piazeeWin->centralWidget())
        {
            TT_captionLabel->setFixedWidth(250);
        }
        else
        {
            TT_captionLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        }
    }
    else if (Event->type() == QEvent::Enter)
    {
        QEnterEvent *mouseEv = static_cast<QEnterEvent*>(Event);
        emit tt_msev_etr(mouseEv);
    }
    else if (Event->type() == QEvent::Leave)
    {
        emit tt_msev_lev(Event);
    }
    return QMainWindow::eventFilter(watched, Event);
}

void NTitleBar::notify(ButtomType Type, QPoint _pos){
    if (_pos.isNull()) _pos = cursor().pos() - pos();
    switch (Type)
    {
        case ButtomType::Mouse_Move:
            if (ms_start.isNull() && _switch["stretch-move-enable"])
            {
                emit tt_hover_mv(_pos);
            }
            else
            {
                emit tt_msev_mv(_pos);
            }
            break;
        case ButtomType::Mouse_Press:
            emit tt_msev_pre(_pos);
            ms_start = _pos;
            break;
        case ButtomType::Mouse_Release:
            emit tt_msev_rls(_pos);
            ms_start = QPoint(0,0);
            break;
        case ButtomType::Mouse_Double:
            emit tt_msev_db();
            break;
    }
}

void NTitleBar::d_filler_color(QList<QColor> color){
    back_color_map["standar_color"].swap(color);
}

void NTitleBar::d_filler_color(QColor color){
    back_color_map["standar_color"] = {color};

}

void NTitleBar::d_filler_color(QString color){
    back_color_map["standar_color"] = {QColor(color)};
}

void NTitleBar::d_filler_pixmap(QString curtain){
    if (!back_pixmap.load(curtain))
    {
        back_pixmap.loadFromData(QByteArray::fromBase64(curtain.toLocal8Bit()));
    }
    else
    {
        back_pixmap.load(curtain);
    }
}

void NTitleBar::d_filler_orientation(NT::CST_BRIGHT_SPOT orien){
    linear_orien = orien;
}

void NTitleBar::d_filler_enable(FillerType Type, bool Enable){
    QList<FillerType> keys = RdoMap_FillerType.keys();
    foreach(FillerType type, keys) RdoMap_FillerType[type] = false;
    RdoMap_FillerType.insert(Type, Enable);
}

void NTitleBar::d_filler_clear(FillerType Type){
    if (Type == FillerType::BACKCOLOR)
    {
        back_color_map["standar_color"].clear();
    }
    else if (Type == FillerType::BACKPIXMAP)
    {
        back_pixmap = QPixmap();
    }
}

void NTitleBar::d_maskfill_transpanrent(QWidget *object, QRect winrect, int radius){
    QPixmap bitmap(winrect.size());
    bitmap.fill(Qt::transparent);
    QPainter ptr(&bitmap);
    QPainterPath path;
    QPoint leftTop(winrect.x(), winrect.y());
    QPoint leftBottom(winrect.x(), winrect.y() + winrect.height() - radius);
    QPoint rightTop(winrect.width(), winrect.y());
    QPoint rightBottom(winrect.width(), winrect.y() + winrect.height() - radius);
    QPoint bottomLeft(winrect.x() + radius, winrect.height());
    QPoint bottomRight(winrect.width() - radius, winrect.height());
    QVector<QPoint> polygonvec;
    polygonvec.append(leftBottom);
    polygonvec.append(leftTop);
    polygonvec.append(rightTop);
    polygonvec.append(rightBottom);
    polygonvec.append(bottomRight);
    polygonvec.append(bottomLeft);
    path.addPolygon(QPolygonF(polygonvec));
//    path.moveTo(leftBottom);
//    path.arcTo(0,
//               winrect.height() - radius,
//               radius,
//               radius,
//               0,
//               360);
//    path.lineTo(bottomLeft);
//    path.lineTo(bottomRight);
//    path.lineTo(rightBottom);
//    path.arcTo(winrect.width() - radius,
//               winrect.y() + winrect.height() - radius,
//               radius,
//               radius,
//               0,
//               360);
//    path.addRoundedRect(winrect, radius + 1, radius + 1);
    ptr.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    ptr.setPen(Qt::NoPen);
    ptr.setBrush(Qt::black);
    ptr.drawPath(path);
//    QLabel *i = new QLabel;
//    i->setPixmap(bitmap);
//    i->show();
//    ptr.fillRect(winrect, Qt::black);
    object->setMask(bitmap.mask());
}

QMainWindow* NTitleBar::piazze(){
    return TT_piazeeWin;
}

void NTitleBar::winmvAndstretchOff(bool enable){
    _switch.insert("stretch-move-enable", enable);
}

QPixmap NTitleBar::freepixmap(QSize size, int graphthickness, QColor graphcolor, GraphType graphtype){
    QPixmap pixmap = QPixmap(size);
    pixmap.fill(Qt::transparent);
    QPainter impactPainter(&pixmap);
    QRect glbrect(impactPainter.window());
    impactPainter.setRenderHints(QPainter::Antialiasing);
    impactPainter.fillRect(glbrect, Qt::transparent);
    QPen ipen = QPen(graphcolor, graphthickness);
    impactPainter.setPen(ipen);
    if (graphtype == GraphType::MINWIN)
    {
        int graph_xstart = 0;
        int graph_ystart = size.height() - graphthickness;
        int graph_width = size.width();
        impactPainter.drawLine(graph_xstart, graph_ystart, graph_xstart + graph_width, graph_ystart);
    }
    else if (graphtype == GraphType::NORWIN)
    {
        QPoint p1(size.width() / 2, 0);
        QPoint p2(size.width() / 2, size.height());
        QPoint p3(size.width(), 0);
        QPoint p4(size.width(), size.height());
        QRect polygon(size.width() / 2, 0, size.width() / 2, size.height());
        impactPainter.drawLine(QPoint(0, size.height() / 2), QPoint(size.width(), size.height() / 2));
        impactPainter.drawArc(polygon, 90 * 16, 180 * 16);
    }
    else if (graphtype == GraphType::MAXWIN)
    {
        ipen.setWidth(graphthickness + 2);
        impactPainter.setPen(ipen);
        impactPainter.drawPolygon(QPolygon(glbrect));
    }
    else if (graphtype == GraphType::QUITE)
    {
        QPoint linetl = QPoint(0, 0);
        QPoint linetr = QPoint(size.width(), 0);
        QPoint linebl = QPoint(0, size.height());
        QPoint linebr = QPoint(size.width(), size.height());
        impactPainter.drawLine(linetl, linebr);
        impactPainter.drawLine(linebl, linetr);
    }
    return pixmap;
}

QPoint NTitleBar::coord(QRect roundrect,int angle){
        double radiusx = (double)roundrect.width() / 2;
        double radiusy = (double)roundrect.height() / 2;
        double round_x = roundrect.x() + radiusx;
        double round_y = roundrect.y() + radiusy;
        double radian = angle * M_PI / 180;
        double endposx = round_x + radiusx * cos(radian);
        double endposy = round_y - radiusy * sin(radian);
        return QPoint(endposx, endposy);
    }

